package com.qczl.weather.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qczl.core.controller.BaseController;
import com.qczl.core.util.DateUtil;
import com.qczl.core.util.PinYinUtil;
import com.qczl.weather.pojo.Info;
import com.qczl.weather.service.InfoService;

/**
 * 天气信息控制器
 * 
 * @author SongR
 *
 */
@Controller
@RequestMapping(value = "/i")
public class InfoController extends BaseController {

	@Resource
	InfoService service;

	/**
	 * 根据城市获取当前时间之后的一周天气
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/city/{cityName}")
	public void getByCity(HttpServletResponse response, @PathVariable String cityName) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html"); // 设置数据格式
		try {
			Info inf = service.getByCity(cityName);
			JSONArray ja = new JSONArray();
			JSONArray j = JSONArray.parseArray(inf.getInfoForecast());
			for (Object o : j) {
				JSONObject ob = (JSONObject) o;
				ob.put("week", DateUtil.getWeekOfDate(DateUtil.convert2Date(ob.getString("date")+" 00:00:00")));
				JSONObject ocond = JSONObject.parseObject(ob.getString("cond"));
				ob.put("img", PinYinUtil.converterToSpell(ocond.getString("txt_d")));
				ja.add(ob);
			}
			response.getWriter().println(ja.toString());
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
	}
}
